package com.humngyang.com.hmy.study.concurrency.myannoations.executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class NewScheduledThreadPoolExample {
    private static Logger log = LoggerFactory.getLogger(NewScheduledThreadPoolExample.class);

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
//        scheduledExecutorService.execute(new Runnable() {public void run() {}}); 第一种没有调度的
  /*      scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                log.info("task {}");
            }
            //延迟三秒后执行
        },3, TimeUnit.SECONDS);*/
//        scheduledExecutorService.shutdown(); 因为是调度，所以不需要关闭资源，如果需要关闭资源的话，那么要根据一定的条件判断
        System.out.println("=======================================");
     /*   scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                log.info("task {}");
            }
            //延迟一秒然后三秒执行一次任务
        }, 1, 3, TimeUnit.SECONDS);*/
        System.out.println("也可以使用Timer来执行任务");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.warn("timer run ");
            }
            //可以使用timer来定时执行，没5秒执行一次
        }, new Date(), 5000);
    }
}
